package com.lamthun.common.client.resp;

import com.lamthun.common.client.CommonCode;
import com.lamthun.common.client.DTO;
import com.lamthun.common.client.ResponseCode;

/**
 * 具体单一返回对象的定义, 对外输出的格式标准如下:
 * {
 * "responseCode" : "1000",
 * "responseMessage" : "ok",
 * "data" : {instance of dto}
 * }
 *
 * @author lamthun
 */
public class CommonResponse<T extends DTO> extends SingleResponse {
    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T extends DTO> CommonResponse<T> buildResponse(String code, String message, T data) {
        CommonResponse<T> response = new CommonResponse<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    public static <T extends DTO> CommonResponse<T> buildResponse(ResponseCode responseCode, T data) {
        return buildResponse(responseCode.getCode(), responseCode.getMessage(), data);
    }

    /**
     * 通用成功返回结果
     */
    public static <T extends DTO> CommonResponse<T> success(T data) {
        return buildResponse(CommonCode.SUCCESS.getCode(), CommonCode.SUCCESS.getMessage(), data);
    }
}
